﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Organizations;
using AbpProjectTemplate.Application.OrganizationUnits.Dtos;
using AbpProjectTemplate.Components.Auditing;
using AbpProjectTemplate.Components.Authorization;
using System.Threading.Tasks;

namespace AbpProjectTemplate.Application.OrganizationUnits
{
    /// <summary>
    /// 组织架构
    /// </summary>
    [AppAuditTag("组织架构")]
    [AppAuthorize(AppPermissions.System.OrganizationUnitManage)]
    public class OrganizationUnitAppService : ApplicationService
    {
        private readonly OrganizationUnitManager _organizationUnitManager;
        private IRepository<OrganizationUnit, long> _organizationUnitRepository;

        public OrganizationUnitAppService(OrganizationUnitManager organizationUnitManager, IRepository<OrganizationUnit, long> organizationUnitRepository)
        {
            this._organizationUnitManager = organizationUnitManager;
            this._organizationUnitRepository = organizationUnitRepository;
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("创建")]
        public async Task Create(OrganizationUnitCreateDto dto)
        {
            var ou = this.ObjectMapper.Map<OrganizationUnit>(dto);
            await this._organizationUnitManager.CreateAsync(ou);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("修改")]
        public async Task Update(OrganizationUnitUpdateDto dto)
        {
            var ou = await this._organizationUnitRepository.GetAsync(dto.Id);
            await this._organizationUnitManager.MoveAsync(dto.Id, dto.ParentId);
            ou.DisplayName = dto.DisplayName;

        }
        /// <summary>
        /// 删除 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AppAudited("删除")]
        public async Task Delete(EntityDto<long> dto)
        {
            await this._organizationUnitManager.DeleteAsync(dto.Id);
        }
    }
}
